
class TUtils645:
    def __init__(self):
        self.version="645"
    @classmethod
    def encode645(self , addr , itemCode3210):
        res =None
        try:
            address = ("000000000000"+addr)[-12:]
            addrArr = [ address[x*2:x*2+2] for x in range(6)]
            addrList  =  [ int(x, 16) for x in addrArr ]
            addrList.reverse()

            itemCodeStr = ("00000000"+itemCode3210)[-8:]
            itemCodeList = [ int(itemCodeStr[x*2:x*2+2],16)+0x33 for x in range(4)]
            itemCodeList.reverse()

            msgList =[0x68] + addrList + [0x68 , 0x11 , 0x04] + itemCodeList
            cs =0
            for i in msgList:
                cs = cs + i
                cs = cs % 256
            msgList = msgList +[cs , 0x16]
            res = bytearray(msgList)
            msgList.clear()
            msgList = None
        except Exception as err:
            print(err,"get645Cmd")
        return res
    @classmethod
    def decode645(self , dataBytes):
        res ={
            "status":0 ,
            "addr":"",
            "itemCode":"",
            "valueStr":""
        }
        try:
            dataList = list(dataBytes)
            #dataList =[0xfe,0x68 ,0x94,0x63,0x66,0x72,0x15,0x00,0x68,0x91,0x08,0x33,0x33,0x34,0x33,0x56,0xc9,0x3b,0x33,0xa7,0x16]
            pos68 = bytearray(dataList).find(0x68)
            pos16 = bytearray(dataList).find(0x16)
            flag = pos68>=0 and pos16>0 and dataList[-1]==0x16
            if flag :
                recFlag = True
                if recFlag :
                    dataList = dataList[pos68:]
                    addrList = dataList[1:7]
                    addrList.reverse()
                    itemList = [x-0x33 for x in dataList[10:14]]
                    itemList.reverse()
                    valueList = [x-0x33 for x in dataList[14:-2]]
                    valueList.reverse()
                    def i16tostr( int16):
                        res = hex(int16).replace("0x","00")
                        return res[-2:]
                    res['addr'] = "".join([i16tostr(x) for x in addrList])
                    res['itemCode'] = "".join([i16tostr(x) for x in itemList])
                    res['valueStr'] = "".join([i16tostr(x) for x in valueList])
                    res['status'] = 1
                else:
                    res['status'] = -1020

            else:
                res['status'] = -1011
        except Exception as err:
            print(err,"get645Cmd")
        return res
    @classmethod
    def getValue(self , valueStr  , negativeFlag):
        res = 0
        try:
            v =0
            strValue = valueStr
            flag = 1
            if negativeFlag>0:
                header = int( strValue[0],16)
                if header & 0x8>0:
                    flag = -1
                    strValue=strValue[1:]
            '''
            if decSize>0 :
                strValue = strValue[:-1*decSize] +"."+strValue[-1*decSize:]
            v = float(strValue)
            if r2!=1 :
                v = v * r2
            strValue = str(v)
            pos = strValue.find('.')
            if pos >=0 :
                pos = pos + decSize
                strValue = strValue[:pos+1]
            '''
            res = flag * float(strValue)
        except Exception as err:
            print(err,"get645Cmd")
        return res
    @classmethod
    def convertValue(self , data , decSize , r1 ,r2 ):
        res = 0
        try:
            if decSize==0:
                res = int( data * r1 * r2)
            else:
                valStr = str(data * r1 * r2)
                pos = valStr.find(".")
                if ( pos > 0):
                    if decSize==0:
                        res = float(valStr[:pos])
                    else:
                        res = float(valStr[:pos+decSize+1])
        except Exception as err:
            print(err,"get645Cmd")
        return res
    @classmethod
    def parse645Value(self , valueStr  , negativeFlag , decSize , rate = 1):
        res = 0
        try:
            v = self.getValue(valueStr , negativeFlag)
            r1 = pow(10 , -1 * decSize)
            v = v * r1 * rate
            vs = f"%.{decSize}f" % v
            res = float(vs)
        except Exception as err:
            print(err,"get645Cmd")
        return res